#
# Copyright (C) Since 2018 ~ 2025, Inc. All Rights Reserved
#
# @Time    : 2025/1/8-下午9:17
# @Author  : Timmy
# @Email   : tangping@cloudstream-tech.com
# @File    : models.py
# @Software: lottery
# @Function:
#

from tortoise import fields
from tortoise.models import Model


class Activity(Model):
    id = fields.IntField(primary_key=True, description=u'主键')
    name = fields.CharField(max_length=20, unique=True, description=u'活动名称')
    date = fields.DateField(description=u'日期')
    desc = fields.CharField(max_length=200, null=True, description=u'备注')
    enable = fields.BooleanField(default=True, description=u'使能')
    create_at = fields.DatetimeField(auto_now_add=True, description=u'创建时间')
    update_at = fields.DatetimeField(auto_now=True, description=u'更新时间')

    def __str__(self):
        return self.name

    class Meta:
        table = 't_activity'


class AwardItem(Model):
    id = fields.IntField(primary_key=True, description=u'主键')
    act = fields.ForeignKeyField(
        model_name='models.Activity', on_delete=fields.CASCADE, related_name='award_activity')
    name = fields.CharField(max_length=20, description=u'奖项名称')
    content = fields.CharField(max_length=80, description=u'奖品')
    count = fields.IntField(default=10, description=u'数量')
    each = fields.IntField(default=5, description=u'一次抽取数量')
    desc = fields.CharField(max_length=200, null=True, description=u'备注')
    enable = fields.BooleanField(default=True, description=u'使能')
    create_at = fields.DatetimeField(auto_now_add=True, description=u'创建时间')
    update_at = fields.DatetimeField(auto_now=True, description=u'更新时间')

    def __str__(self):
        return self.name

    class Meta:
        table = 't_award'
        unique_together = (('act', 'name'),)


class People(Model):
    id = fields.IntField(primary_key=True, description=u'主键')
    act = fields.ForeignKeyField(
        model_name='models.Activity', on_delete=fields.CASCADE, related_name='people_activity')
    name = fields.CharField(max_length=20, description=u'姓名')
    dep = fields.CharField(max_length=20, null=True, description=u'部门')
    identity = fields.CharField(max_length=40, null=True, description=u'身份')
    icon = fields.CharField(max_length=200, null=True, description=u'头像')
    alpha = fields.IntField(default=1)
    enable = fields.BooleanField(default=True, description=u'使能')
    create_at = fields.DatetimeField(auto_now_add=True, description=u'创建时间')
    update_at = fields.DatetimeField(auto_now=True, description=u'更新时间')

    def __str__(self):
        return self.name

    def __json__(self):
        return {"name": self.name, "dep": self.dep, "icon": self.icon}

    class Meta:
        table = 't_people'
        unique_together = (('act', 'name'),)


class ShotEvent(Model):
    id = fields.IntField(primary_key=True, description=u'主键')
    eid = fields.UUIDField(description=u'事件ID')
    act = fields.ForeignKeyField(
        model_name='models.Activity', on_delete=fields.CASCADE, related_name='shot_activity'
    )
    who = fields.ForeignKeyField(
        model_name='models.People', on_delete=fields.CASCADE, related_name='shot_events_who')
    item = fields.ForeignKeyField(
        model_name='models.AwardItem', on_delete=fields.CASCADE, related_name='shot_events_item'
    )
    create_at = fields.DatetimeField(auto_now_add=True, description=u'创建时间')
    cancel_at = fields.DatetimeField(index=True, null=True, description=u'取消时间')

    def __str__(self):
        return self.who

    class Meta:
        table = 't_shot_event'
